package com.lt.flashcard.view;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zul.event.PagingEvent;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.Cart;
import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.entity.Productcategory;
import com.lt.flashcard.model.service.EcommerceService;
import com.lt.flashcard.model.service.Repository;

public class StoreVm {

    private List<Productcategory> categories = null;

    @Init
    public void init(@ContextParam(ContextType.PAGE) Page page) {
        EcommerceService srv = Repository.getInstance().getEcommerceService();
        String cat = Executions.getCurrent().getParameter("cat");
        if (StringUtils.isNotBlank(cat)) {
            Productcategory pro = srv.getProductCategoryBySlug(cat);
            if (pro != null) {
                category = pro.getId();
                Utility.addOgTag(page, String.format("Bộ sản phẩm %s", pro.getName()), String.format("Các sản phẩm trong bộ %s", pro.getName()), "");
            } else {
                category = -1l;
                Utility.addOgTag(page, "Sản phẩm của Blueup", "Các sản phẩm hiện có của Blueup", "");
            }
        } else {
            Utility.addOgTag(page, "Sản phẩm của Blueup", "Các sản phẩm hiện có của Blueup", "");
        }
        loadFlashcards(true);
        purchased = srv.getPurchasedProductsId(Utility.getUser());
    }

    public List<Productcategory> getCategories() {
        if (categories == null) {
            EcommerceService srv = Repository.getInstance().getEcommerceService();
            categories = srv.getParentProductCategories();
        }
        return categories;
    }

    public void setCategories(List<Productcategory> categories) {
        this.categories = categories;
    }

    private List<FlashcardSet> flashcards = new ArrayList<FlashcardSet>();
    private Set<Long> purchased = new HashSet<Long>();

    public List<FlashcardSet> getFlashcards() {
        return flashcards;
    }

    public void setFlashcards(List<FlashcardSet> flashcards) {
        this.flashcards = flashcards;
    }

    private Long category = new Long(0);

    public Long getCategory() {
        return category;
    }

    public void setCategory(Long category) {
        this.category = category;
    }

    @NotifyChange("flashcards")
    @Command
    public void selectCategory(@BindingParam("catid") Long catid) {
        /*this.category = catid;
        loadFlashcards(true);*/

        EcommerceService srv = Repository.getInstance().getEcommerceService();
        Productcategory pro = srv.getProductCategory(catid);
        if (pro != null) {
            // TODO use Ajax to reload
            String url = Utility.removeContextPath(Utility.generateUrl("store", null, pro.getSlug()));
            Utility.redirect(url, true);
        }
    }

    private int page = 0;
    private int total = 0;
    private static final int PAGE_SIZE = 15;

    /**
     * Do filter flashcards with pager enabled
     * @param search
     * @param page
     */
    public void loadFlashcards(boolean newsearch) {
        if (newsearch) {
            page = 0;
        }

        EcommerceService srv = Repository.getInstance().getEcommerceService();
        flashcards = srv.getProductByFilter("", this.category, false, false, PAGE_SIZE, page);
        if (newsearch) {
            total = srv.countProductByFilter("", this.category, false, false);
            BindUtils.postNotifyChange(null, null, this, "total");
        }
    }

    @NotifyChange("flashcards")
    @Command
    public void paging(@BindingParam("event") PagingEvent event) {
        page = event.getActivePage();
        loadFlashcards(false);
    }

    /**
     * Get the card
     * @return
     */
    public Cart getCart() {
        EcommerceService srv = Repository.getInstance().getEcommerceService();
        return srv.getCurrentCart();
    }

    /**
     * Add a product to cart
     */
    @NotifyChange({ "cart", "flashcards" })
    @Command
    public void addToBasket(@BindingParam("productid") Long productid) {
        Cart cart = getCart();
        if (cart != null) {
            cart.addItem(productid);
        }
    }

    /**
     * @return the purchased
     */
    public Set<Long> getPurchased() {
        return purchased;
    }

    /**
     * @param purchased the purchased to set
     */
    public void setPurchased(Set<Long> purchased) {
        this.purchased = purchased;
    }

    /**
     * @return the page
     */
    public int getPage() {
        return page;
    }

    /**
     * @param page the page to set
     */
    public void setPage(int page) {
        this.page = page;
    }

    /**
     * @return the total
     */
    public int getTotal() {
        return total;
    }

    /**
     * @param total the total to set
     */
    public void setTotal(int total) {
        this.total = total;
    }
}
